package com.quktzhuanka.app.https;

import android.content.Context;
import android.os.Build;
import android.util.Log;


import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.BinaryHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.quktzhuanka.app.CaiNiaoApplication;
import com.quktzhuanka.app.common.SPUtils;
import com.quktzhuanka.app.config.Constants;

import java.io.UnsupportedEncodingException;

import cz.msebera.android.httpclient.conn.ssl.SSLSocketFactory;
import cz.msebera.android.httpclient.entity.ByteArrayEntity;
import cz.msebera.android.httpclient.message.BasicHeader;
import cz.msebera.android.httpclient.protocol.HTTP;


/**
 * @author Lin 网络请求工具类
 */
//

public class HttpUtils {

    public static final String TAG = "HttpUtils";
    private static AsyncHttpClient client = new AsyncHttpClient(); // 实例化对象

//    private static String imei;

    static {
        // 获取手机设备的唯一码(IMIE )
//        final TelephonyManager TelephonyMgr = (TelephonyManager) (CaiNiaoApplication.getAppContext()).getSystemService(Context.TELEPHONY_SERVICE);
        //动态获取权限

//        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
//            imei = TelephonyMgr.getDeviceId();
//        } else {
//            Acp.getInstance(CaiNiaoApplication.getAppContext()).request(new AcpOptions.Builder().setPermissions(android.Manifest.permission.READ_PHONE_STATE).build(), new AcpListener() {
//                @Override
//                public void onGranted() {
//                    imei = TelephonyMgr.getDeviceId();
//                }
//
//                @Override
//                public void onDenied(List<String> permissions) {
//                    Toast.makeText(CaiNiaoApplication.getAppContext(), permissions.toString() + "权限拒绝", Toast.LENGTH_SHORT).show();
//                }
//            });
//
//        }


//        if (TextUtils.isEmpty(imei)) {
//            imei = "77";// 默认值
//        }
        try {
            String versionCode = Build.VERSION.RELEASE; // 设备的系统版本
//            String versionCode = String.valueOf(SystemUtil.getVersionCode(CarZoneApplication.getAppContext()));
            client.addHeader("version", versionCode);// 系统版本

            client.addHeader("platform", "Android安卓");//app类型iOS苹果、Android安卓
        } catch (Exception e) {
//            LogUtils.e(TAG, "获取系统版本出错！");
            e.printStackTrace();
        }
        client.setTimeout(41000); // 设置链接超时，如果不设置，默认为10s

        //HTTPS
        SSLSocketFactory sf = SSLSocketFactory.getSocketFactory();
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); // 允许所有主机的验证

        //SSLSocketFactory sf = MySSLSocketFactory.getSocketFactory();
        //sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); // 允许所有主机的验证
        client.setSSLSocketFactory(sf);

    }


    public static void get(String urlString, AsyncHttpResponseHandler res) // 用一个完整url获取一个string对象
    {
        String token = SPUtils.getStringData(CaiNiaoApplication.getAppContext(), "token", "");
        client.addHeader("Content-type", "text/html;charset=UTF-8");
        client.addHeader("token", token);
        client.get(urlString, res);
    }

    /**
     * post提交
     *
     * @param urlString 接口地址
     * @param params
     * @param res
     */
    public static void post(String urlString, RequestParams params, AsyncHttpResponseHandler res) {
        String token = SPUtils.getStringData(CaiNiaoApplication.getAppContext(), Constants.TOKEN, "");
        client.addHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");
        client.addHeader("token", token);
        params.put("token", token);
        Log.d("dsafsd",params.toString());
        client.post(urlString, params, res);
    }
    public static void post1(String urlString, RequestParams params, AsyncHttpResponseHandler res) {
        client.addHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");
        client.post(urlString, params, res);
    }
    /**
     * post提交
     *
     * @param urlString 接口地址
     * @param params
     * @param res
     */
    public static void postUpload(String urlString, RequestParams params, AsyncHttpResponseHandler res) {
        AsyncHttpClient client1 = new AsyncHttpClient();
        String token = SPUtils.getStringData(CaiNiaoApplication.getAppContext(), "token", "");
        client1.addHeader("token", token);
        params.put("token", token);
        client1.post(urlString, params, res);
    }

    public static void get(String urlString, RequestParams params, AsyncHttpResponseHandler res) // url里面带参数
    {
        String token = SPUtils.getStringData(CaiNiaoApplication.getAppContext(), "token", "");
        client.addHeader("Content-type", "text/html;charset=UTF-8");
        client.addHeader("token", token);
        params.put("token", token);
        client.get(urlString, params, res);
    }

    /**
     * @param urlString
     * @param params
     * @param res
     */
    public static void post(boolean is_loginorregister, String urlString, RequestParams params, AsyncHttpResponseHandler res) // url里面带参数
    {
        String token = SPUtils.getStringData(CaiNiaoApplication.getAppContext(), "token", "");

        client.addHeader("token", token);
//        client.addHeader("Content-type","text/html;charset=UTF-8");//application/x-www-form-urlencoded
        client.addHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");
        params.put("token", token);
        client.post(urlString, params, res);
    }

    public static void get(String urlString, JsonHttpResponseHandler res) // 不带参数，获取json对象或者数组
    {
        String token = SPUtils.getStringData(CaiNiaoApplication.getAppContext(), "token", "");

        client.addHeader("Content-type", "text/html;charset=UTF-8");
        client.addHeader("token", token);
        client.get(urlString, res);
    }

    public static void post(String urlString, JsonHttpResponseHandler res) // 不带参数，获取json对象或者数组
    {
        String token = SPUtils.getStringData(CaiNiaoApplication.getAppContext(), "token", "");
//        if (TextUtils.isEmpty(token)) {
//            Intent intent = new Intent(CaiNiaoApplication.getAppContext(), LoginActivity.class);
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            CaiNiaoApplication.getAppContext().startActivity(intent);
//            return;
//        }
        client.addHeader("Content-type", "text/html;charset=UTF-8");
        client.addHeader("token", token);
        client.post(urlString, res);
    }

    public static void get(String urlString, RequestParams params, JsonHttpResponseHandler res) // 带参数，获取json对象或者数组
    {
        String token = SPUtils.getStringData(CaiNiaoApplication.getAppContext(), "token", "");

        client.addHeader("Content-type", "text/html;charset=UTF-8");
        client.addHeader("token", token);
        params.put("token", token);
        client.get(urlString, params, res);
    }

    public static void post(String urlString, RequestParams params, JsonHttpResponseHandler res) // 带参数，获取json对象或者数组
    {
        String token = SPUtils.getStringData(CaiNiaoApplication.getAppContext(), "token", "");

        client.addHeader("Content-type", "application/json;charset=UTF-8");
//        client.addHeader("Content-type","application/json");

        client.addHeader("token", token);
        params.put("token", token);
        client.post(urlString, params, res);
    }

    //TODO json提交数据
    public static void post(Context mContext, String urlString, String jsonString, JsonHttpResponseHandler res) // 带参数，获取json对象或者数组
    {
        String token = SPUtils.getStringData(CaiNiaoApplication.getAppContext(), "token", "");

        client.addHeader("token", token);
        ByteArrayEntity entity = null;
//        try {
//            entity = new ByteArrayEntity(jsonString.getBytes("UTF-8"));
//            entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded;charset=UTF-8"));
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//        client.post(mContext,urlString,entity,"application/x-www-form-urlencoded;charset=UTF-8",res);
        try {
            entity = new ByteArrayEntity(jsonString.getBytes("UTF-8"));
            entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        client.post(mContext, urlString, entity, "application/json", res);
    }

    public static void get(String uString, BinaryHttpResponseHandler bHandler) // 下载数据使用，会返回byte数据
    {
        String token = SPUtils.getStringData(CaiNiaoApplication.getAppContext(), "token", "");

        client.addHeader("token", token);
        client.get(uString, bHandler);
    }

    public static void post(String uString, BinaryHttpResponseHandler bHandler) // 下载数据使用，会返回byte数据
    {
        String token = SPUtils.getStringData(CaiNiaoApplication.getAppContext(), "token", "");

        client.addHeader("Content-type", "text/html;charset=UTF-8");
        client.addHeader("token", token);
        client.post(uString, bHandler);
    }

}
